package com.siruier.boss.ui.adapter

import android.content.Intent
import android.net.Uri
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.siruier.boss.api.BannerBean
import com.siruier.boss.ui.activity.main.MainActivity
import com.siruier.boss.ui.activity.mall.MallGoodsActivity
import com.siruier.boss.ui.ext.dp
import com.siruier.boss.ui.ext.startActivityExt
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class BossBannerImageAdapter(
    rounded: Int = 0
) : BannerImageAdapter<BannerBean>(null) {

    private val mRequestOptions: RequestOptions =
        if (rounded > 0) {
            RequestOptions().transform(CenterCrop(), RoundedCorners(rounded.dp))
        } else {
            RequestOptions().transform(CenterCrop())
        }

    override fun onBindView(holder: BannerImageHolder, data: BannerBean, position: Int, size: Int) {
        Glide.with(holder.itemView)
            .load(data.url)
            .apply(mRequestOptions)
            .into(holder.imageView)
        holder.itemView.setOnClickListener {
            kotlin.runCatching {
                if (data.linkType == 1) {
                    holder.imageView.context.startActivityExt<MallGoodsActivity> {
                        putExtra("mGoodsId", data.link.toInt())
                    }
                } else if (data.linkType == 2) {
                    holder.imageView.context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(data.link)))
                } else if (data.linkType == 3) {
                    holder.imageView.context.startActivityExt<MainActivity> {
                        putExtra("changePage", data.link)
                    }
                }
            }
        }
    }
}